using System;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using DataTools.PagingUtils;
using System.Windows.Forms;
using Lib;

namespace AccessLayer
{
    class SqlProvider:DataProvider
    {

         #region Fields
        private static readonly string _connectionString;
        #endregion

        #region Contructors
        static SqlProvider()
        {
            _connectionString = ConnInfo.cnn();//System.Configuration.ConfigurationManager.ConnectionStrings["cnn_" + ConnInfo.cnn()].ConnectionString;           
        }
        #endregion

        #region Methods


        protected static SqlConnection getConnect1()
        {
            return new SqlConnection(ConnInfo.cnn());
            //return new SqlConnection("Data Source=" + ConnInfo.Server + ";Initial Catalog="
            //    + ConnInfo.Data + ";User ID=" + ConnInfo.User + ";password=" + ConnInfo.Pwd + ";Pooling=True");
        }
        protected static SqlConnection getConnect()
        {
            return new SqlConnection(ConnInfo.cnn());
            //return new SqlConnection("Data Source=" + ConnInfo.Server + ";Initial Catalog="
            //    + ConnInfo.Data + ";User ID=" + ConnInfo.User + ";password=" + ConnInfo.Pwd + ";Pooling=True");
            //return new SqlConnection("Data Source=" + data + ";Initial Catalog=qlbia;Integrated Security=True");
             
        }
        protected static SqlConnection getConnect2()
        {
            return new SqlConnection(ConnInfo.cnn());
            //return new SqlConnection("Data Source=" + ConnInfo.Server + ";Initial Catalog="
            //     + ConnInfo.Data + ";User ID=" + ConnInfo.User + ";password=" + ConnInfo.Pwd + ";Pooling=True");

        }
      
       
        public override int ExecuteNonQuery(string spName, params object[] parameterValues)
        {
            return SqlHelper.ExecuteNonQuery(getConnect(), spName, parameterValues);
        }
        
        public override IDataReader ExecuteReader(string spName, params object[] parameterValues)
        {
            return SqlHelper.ExecuteReader(getConnect(), spName, parameterValues);    
        }        
        public override IDataReader getAll(object tableName, string orderBy)
        {
            return ExecuteReader(StoreProcedureName.GetAll, tableName.ToString(), orderBy);
        }       
        public override IDataReader Find(object tableName, object columnName, object key)
        {
            return ExecuteReader(StoreProcedureName.Find,tableName.ToString(), columnName.ToString(), key.ToString());
        }      
        public override int InsertUpdateDelete(ProviderAction action, string spName, string outPutName, params object[] parameterValues)
        {
            using (SqlConnection cnn = getConnect())
            {
                SqlCommand cmd = SqlHelper.CreateCommand(cnn, spName, parameterValues);
                cmd.CommandTimeout = 0;
                cnn.Open();
                int rs = cmd.ExecuteNonQuery();
                cnn.Close();
                if (rs > 0 && action == ProviderAction.Insert && !String.IsNullOrEmpty(outPutName))
                {
                    int n = 0;
                    if (int.TryParse(cmd.Parameters[outPutName].Value.ToString(), out n))
                        return Conversion.ConvertToInt(cmd.Parameters[outPutName].Value.ToString());
                    else
                        return rs;
                }
                return rs;
            }
        }
        #endregion
       
    }
}
